//package com.ketangpai.admin.service.impl;
//
//import com.ketangpai.admin.dao.StuWorkDao;
//import com.ketangpai.admin.dao.WorkDao;
//import com.ketangpai.admin.entity.StuWork;
//import com.ketangpai.admin.entity.Work;
//import com.ketangpai.admin.entity.dto.StuWorkDTO;
//import com.ketangpai.admin.mapper.StuWorkMapper;
//import com.ketangpai.admin.service.StuWorkService;
//import com.github.pagehelper.Page;
//import com.github.pagehelper.PageHelper;
//import com.ketangpai.admin.utils.AliOSSUtil;
//import org.mybatis.logging.Logger;
//import org.mybatis.logging.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//
//import javax.annotation.Resource;
//import java.util.ArrayList;
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//
//@Service
//public class StuWorkServiceImpl implements StuWorkService {
//    private static final Logger log = LoggerFactory.getLogger(StuWorkServiceImpl.class);
//
//    @Autowired
//    private StuWorkMapper stuWorkMapper;
//    private StuWorkDao stuWorkDao;
//    private WorkDao workDao;
//    @Resource
//    private AliOSSUtil aliOSSUtil;
//    public StuWorkServiceImpl(StuWorkMapper stuWorkMapper, StuWorkDao stuWorkDao, WorkDao workDao) {
//        this.stuWorkMapper = stuWorkMapper;
//        this.stuWorkDao = stuWorkDao;
//        this.workDao = workDao;
//    }
//
//    @Override
//    public Map<String, Object> getStudentWorks(Integer workId, Integer status, String studentName, Integer page, Integer pageSize) {
//
//        // 参数校验
//        if (workId == null) {
//            return new HashMap<>();
//        }
//
//        try {
//            // 开启分页
//            PageHelper.startPage(page != null ? page : 1, pageSize != null ? pageSize : 10);
//
//            // 查询数据
//            List<StuWork> list = stuWorkMapper.getStudentWorks(workId, status, studentName);
//
//            // 获取分页信息
//            Page<StuWork> pageInfo = (Page<StuWork>) list;
//
//            // 获取各状态数量并缓存结果
//            int unsubmittedCount = stuWorkMapper.countByStatus(workId, 0);
//            int submittedCount = stuWorkMapper.countByStatus(workId, 1);
//            int gradedCount = stuWorkMapper.countByStatus(workId, 2);
//
//
//            // 组装返回结果
//            Map<String, Object> result = new HashMap<>();
//            result.put("list", list != null ? list : new ArrayList<>());
//            result.put("total", pageInfo.getTotal());
//            result.put("unsubmittedCount", unsubmittedCount);
//            result.put("submittedCount", submittedCount);
//            result.put("gradedCount", gradedCount);
//
//            return result;
//        } catch (Exception e) {
//            return new HashMap<>();
//        }
//    }
//
//    @Override
//    public boolean submitWorkGrade(Integer id, String score, String message) {
//        try {
//            return stuWorkMapper.updateWorkGrade(id, score, message) > 0;
//        } catch (Exception e) {
//            e.printStackTrace();
//            return false;
//        }
//    }
//    @Override
//    public StuWork getStudentWorkDetail(Integer id) {
//        return stuWorkMapper.getStudentWorkDetail(id);
//    }
//
//    @Override
//    public boolean expediteSubmission(Integer id) {
//        try {
//            return stuWorkMapper.updateExpediteCount(id) > 0;
//        } catch (Exception e) {
//            e.printStackTrace();
//            return false;
//        }
//    }
//    @Override
//    public boolean updateWorkStatus(Integer id, Integer status) {
//        try {
//            return stuWorkMapper.updateWorkStatus(id, status) > 0;
//        } catch (Exception e) {
//            e.printStackTrace();
//            return false;
//        }
//    }
//
//    @Override
//    public StuWork queryById(Integer id) {
//        return stuWorkDao.selectById(id);
//    }
//    /**
//     * 根据用户、课程id查询作业
//     */
//    @Override
//    public List<StuWorkDTO> queryByUCid(Integer cid, Integer uid) {
//        //先根据课程id找到对应的作业
//        List<Work> works=workDao.selectByCourseId(cid);
//        List<StuWorkDTO> stuWorks=new ArrayList<>();
//        for (int i=0;i<works.size();i++){
//            Work work=works.get(i);
//            System.out.println(work);
//            if(work.getStatus()==2) {
//                StuWorkDTO stuWorkDTO = stuWorkDao.queryById(work.getId(),uid);
//                stuWorkDTO.setActivity(work.getActivity());
//                stuWorkDTO.setTitle(work.getTitle());
//                stuWorkDTO.setType(work.getType());
//                stuWorkDTO.setEndTime(work.getEndTime());
//                stuWorks.add(stuWorkDTO);
//            }
//        }
//        return stuWorks;
//    }
//
//    @Override
//    public boolean update(StuWork stuWork) {
//        System.out.println(stuWork);
//        if (stuWork.getFile() != null) {
//            String url = aliOSSUtil.upload(stuWork.getFile());
//            stuWork.setFilename(url);
//        }
//        return stuWorkDao.updateById(stuWork) > 0;
//    }
//}
package com.ketangpai.admin.service.impl;

import com.ketangpai.admin.dao.StuWorkDao;
import com.ketangpai.admin.dao.WorkDao;
import com.ketangpai.admin.entity.StuWork;
import com.ketangpai.admin.entity.Work;
import com.ketangpai.admin.entity.dto.StuWorkDTO;
import com.ketangpai.admin.mapper.StuWorkMapper;
import com.ketangpai.admin.service.StuWorkService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ketangpai.admin.utils.AliOSSUtil;
import org.mybatis.logging.Logger;
import org.mybatis.logging.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StuWorkServiceImpl implements StuWorkService {
    private static final Logger log = LoggerFactory.getLogger(StuWorkServiceImpl.class);

    @Autowired
    private StuWorkMapper stuWorkMapper;
    private StuWorkDao stuWorkDao;
    private WorkDao workDao;
    @Resource
    private AliOSSUtil aliOSSUtil;
    public StuWorkServiceImpl(StuWorkMapper stuWorkMapper, StuWorkDao stuWorkDao, WorkDao workDao) {
        this.stuWorkMapper = stuWorkMapper;
        this.stuWorkDao = stuWorkDao;
        this.workDao = workDao;
    }

    @Override
    public Map<String, Object> getStudentWorks(Integer workId, Integer status, String studentName, Integer page, Integer pageSize) {

        // 参数校验
        if (workId == null) {
            return new HashMap<>();
        }

        try {
            // 开启分页
            PageHelper.startPage(page != null ? page : 1, pageSize != null ? pageSize : 10);

            // 查询数据
            List<StuWork> list = stuWorkMapper.getStudentWorks(workId, status, studentName);

            // 获取分页信息
            Page<StuWork> pageInfo = (Page<StuWork>) list;

            // 获取各状态数量并缓存结果
            int unsubmittedCount = stuWorkMapper.countByStatus(workId, 0);
            int submittedCount = stuWorkMapper.countByStatus(workId, 1);
            int gradedCount = stuWorkMapper.countByStatus(workId, 2);


            // 组装返回结果
            Map<String, Object> result = new HashMap<>();
            result.put("list", list != null ? list : new ArrayList<>());
            result.put("total", pageInfo.getTotal());
            result.put("unsubmittedCount", unsubmittedCount);
            result.put("submittedCount", submittedCount);
            result.put("gradedCount", gradedCount);

            return result;
        } catch (Exception e) {
            return new HashMap<>();
        }
    }

    @Override
    public boolean submitWorkGrade(Integer id, String score, String message) {
        try {
            return stuWorkMapper.updateWorkGrade(id, score, message) > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    @Override
    public StuWork getStudentWorkDetail(Integer id) {
        return stuWorkMapper.getStudentWorkDetail(id);
    }

    @Override
    public boolean expediteSubmission(Integer id) {
        try {
            return stuWorkMapper.updateExpediteCount(id) > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    @Override
    public boolean updateWorkStatus(Integer id, Integer status) {
        try {
            return stuWorkMapper.updateWorkStatus(id, status) > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public StuWork queryById(Integer id) {
        return stuWorkDao.selectById(id);
    }
    /**
     * 根据用户、课程id查询作业
     */
    @Override
    public List<StuWorkDTO> queryByUCid(Integer cid, Integer uid) {
        // 先根据课程id找到对应的作业
        List<Work> works = workDao.selectByCourseId(cid);
        List<StuWorkDTO> stuWorks = new ArrayList<>();

        for (Work work : works) {
            if (work.getStatus() == 2) {
                // 查询学生作业详情
                StuWorkDTO stuWorkDTO = stuWorkDao.queryById(work.getId(), uid);

                // 检查 stuWorkDTO 是否为 null
                if (stuWorkDTO != null) {
                    stuWorkDTO.setActivity(work.getActivity());
                    stuWorkDTO.setTitle(work.getTitle());
                    stuWorkDTO.setType(work.getType());
                    stuWorkDTO.setEndTime(work.getEndTime());
                    stuWorks.add(stuWorkDTO);
                } else {
                    // 使用 Supplier<String> 记录日志
                    log.warn(() -> String.format("No StuWorkDTO found for workId: %d and userId: %d", work.getId(), uid));
                }
            }
        }
        return stuWorks;
    }

    @Override
    public boolean update(StuWork stuWork) {
        System.out.println(stuWork);
        if (stuWork.getFile() != null) {
            String url = aliOSSUtil.upload(stuWork.getFile());
            stuWork.setFilename(url);
        }
        return stuWorkDao.updateById(stuWork) > 0;
    }


}